<?php
require_once "../load.php";
error_reporting(0);
$action = $_POST["action"];
unset($_POST["action"]);
if (get_magic_quotes_gpc()) {
    $_POST = Owl_Format::stripslashes_deep($_POST);
}

if (!defined("DEFAULT_SITE_LOGO")) {
    define("DEFAULT_SITE_LOGO", "images/logo/owl-logo-light-96.png");
}
define("LOGO_DIR", "user-images");
define("LOGO_PATH", UPLOAD_PATH . LOGO_DIR . "/");

switch ($action) {
    case 'system_update' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        $options_data = array();
        $now = Owl_Date_Time::current_time('timestamp');

        //We will upload the logo first
        if ($_POST["use_default_logo"] == "on") {
            $new_logo = DEFAULT_SITE_LOGO;
        }
        else {
            $logo_data = $_FILES["site_logo"];

            //If uploading return error, then die
            if ($logo_data["error"] != 0 && $logo_data["error"] != 4) {
                echo '<div class="ajax-error">';
                echo __("Error while uploading file, your data is not affected by this error.") . "<br />";
                echo Owl_Functions::get_upload_error($logo_data["error"]);
                echo '</div>';
                die();
            }
            
            elseif ($logo_data['error'] == 0 && is_uploaded_file($logo_data['tmp_name'])) {
                $logo_file_info = pathinfo($logo_data['name']);

                //Check file type
                if (!Owl_Functions::file_upload_is_image($logo_data)) {
                    echo '<div class="ajax-error">';
                    echo __("Uploaded file is not an image, please upload following file format: JPG, GIF or PNG");
                    echo '</div>';
                    die();
                }

                $logo_file_name = $now . "." . $logo_file_info["extension"];
                $logo_path = LOGO_PATH . $logo_file_name;
                $new_logo = UPLOAD_DIR . '/' . LOGO_DIR . "/" . $logo_file_name;
            }
            else {
                $new_logo = get_option("site_logo", DEFAULT_SITE_LOGO);
            }
        }

        //Update the site logo option
        $old_logo = get_option("site_logo");
        if (!update_option("site_logo", $new_logo)) {
            echo '<div class="ajax-error">';
            echo __("Error while updating system options: ");
            echo get_option_error();
            echo '</div>';
            die();
        }

        //Now attempt to delete the old logo and move the new one to logo folder
        if ($old_logo != DEFAULT_SITE_LOGO && ($logo_data["error"] == 0 || $_POST["use_default_logo"] == "on")) {
            if (!@unlink(APS_PATH . $old_logo)) {
                echo '<div class="ajax-error">';
                echo __("Error while updating system options: ") . "<br />";
                echo sprintf(__("Cannot delete file %s"), $old_logo);
                echo '</div>';
                update_option("site_logo", $old_logo);
                die();
            }
        }

        if ($_POST["use_default_logo"] != "on" && $logo_data["error"] == 0) {
            $logo_path = LOGO_PATH . $logo_file_name;
            if (!@move_uploaded_file($logo_data["tmp_name"], $logo_path)) {
                echo '<div class="ajax-error">';
                echo __("Error while updating system options: ") . "<br />";
                echo sprintf(__("Cannot create file %s in folder %s"), $logo_file_name, UPLOAD_DIR . '/' . LOGO_DIR);
                echo '</div>';
                update_option("site_logo", $old_logo);
                die();
            }
        }

        //Update all options
        $options_data["site_name"] = trim(Owl_Format::esc_html($_POST["site_name"]));
        $options_data["site_description"] = trim(Owl_Format::esc_html($_POST["site_description"]));
        $options_data["language"] = $_POST["language"];
        $options_data["gmt_offset"] = $_POST["gmt_offset"];
        $options_data["content_per_page"] = $_POST["content_per_page"];
        $options_data["max_pages_display"] = $_POST["max_pages_display"];
        $options_data["widget_lock"] = ($_POST["widget_lock"] == "on") ? 1 : 0;
        $options_data["site_effect"] = ($_POST["site_effect"] == "on") ? 1 : 0;
        $options_data["home_template"] = $_POST["home_template"];
        $options_data["product_sort"] = $_POST["product_sort"];

        if (update_options_list($options_data)) {
            echo 1;
        }
        else {
            echo '<div class="ajax-error">';
            echo __("Error while updating system options: ");
            echo get_option_error();
            echo '</div>';
            die();
        }

        break;
}